package com.jprocms.module.cms.controller.admin.model;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.excel.core.util.ExcelUtils;
import com.jprocms.module.cms.controller.admin.model.vo.*;
import com.jprocms.module.cms.convert.model.ModelConvert;
import com.jprocms.module.cms.dal.dataobject.model.ModelDO;
import com.jprocms.module.cms.service.model.ModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 模型")
@RestController
@RequestMapping("/cms/model")
@Validated
public class ModelController {

    @Resource
    private ModelService modelService;

    @PostMapping("/create")
    @Operation(summary = "创建模型")
    @PreAuthorize("@ss.hasPermission('cms:model:create')")
    public CommonResult<Long> createModel(@Valid @RequestBody ModelCreateReqVO createReqVO) {
        return success(modelService.createModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模型")
    @PreAuthorize("@ss.hasPermission('cms:model:update')")
    public CommonResult<Boolean> updateModel(@Valid @RequestBody ModelUpdateReqVO updateReqVO) {
        modelService.updateModel(updateReqVO);
        return success(true);
    }

    @PostMapping("/updateField")
    @Operation(summary = "更新模型")
    @PreAuthorize("@ss.hasPermission('cms:model:update')")
    public CommonResult<Boolean> updateModelField(@Valid @RequestBody ModelUpdateFieldReqVO updateReqVO) {
        modelService.updateModelField(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:model:delete')")
    public CommonResult<Boolean> deleteModel(@RequestParam("id") Long id) {
        modelService.deleteModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:model:query')")
    public CommonResult<ModelRespVO> getModel(@RequestParam("id") Integer id) {
        ModelDO model = modelService.getModel(id);
        return success(ModelConvert.INSTANCE.convert(model));
    }

    @GetMapping("/listByIds")
    @Operation(summary = "获得模型列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('cms:model:query')")
    public CommonResult<List<ModelRespVO>> getModelList(@RequestParam("ids") Collection<Integer> ids) {
        List<ModelDO> list = modelService.getModelList(ids);
        return success(ModelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模型分页")
    @PreAuthorize("@ss.hasPermission('cms:model:query')")
    public CommonResult<PageResult<ModelRespVO>> getModelPage(@Valid ModelPageReqVO pageVO) {
        PageResult<ModelDO> pageResult = modelService.getModelPage(pageVO);
        return success(ModelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/list")
    @Operation(summary = "查询模型部不分页")
    public CommonResult<List<ModelRespVO>> getModelList(@Valid ModelQueryReqVO exportReqVO,
                                                        HttpServletResponse response) throws IOException {
        List<ModelDO> list = modelService.getModelList(exportReqVO);
        return success(ModelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模型 Excel")
    @PreAuthorize("@ss.hasPermission('cms:model:export')")

    public void exportModelExcel(@Valid ModelQueryReqVO exportReqVO,
                                 HttpServletResponse response) throws IOException {
        List<ModelDO> list = modelService.getModelList(exportReqVO);
        // 导出 Excel
        List<ModelExcelVO> datas = ModelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "模型.xls", "数据", ModelExcelVO.class, datas);
    }

}
